#include "stdafx.h"
#include "Helper.h"
#include "primitive_datatypes.h"
#include "Desc.h"
#include "RenderEngine.h"
#include "D3D9RenderEngine.h"

//##########################################################
CD3D9RenderEngine::CD3D9RenderEngine(){
	this->_D3D9	= NULL;
	this->_D3D9Dev = NULL;
	this->_Background = NULL;

	//this->_SceneGraph = new std::vector<int*>;
}
//##########################################################
void CD3D9RenderEngine::Init(RenderEngineInitDesc& desc){

	this->_D3D9 = Direct3DCreate9(D3D_SDK_VERSION);

	assert(this->_D3D9);

	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp,sizeof(d3dpp));

	if(desc.FullScreen){
		d3dpp.Windowed			= false;
		d3dpp.SwapEffect		= D3DSWAPEFFECT_DISCARD;
		d3dpp.BackBufferFormat	= D3DFMT_A8R8G8B8;
		d3dpp.BackBufferCount	= 1;
		d3dpp.BackBufferHeight	= desc.Height;
		d3dpp.BackBufferWidth	= desc.Width;
		d3dpp.hDeviceWindow		= desc.WindowInstance;
	}else{
		d3dpp.Windowed			= true;
		d3dpp.SwapEffect		= D3DSWAPEFFECT_DISCARD;
		d3dpp.BackBufferFormat	= D3DFMT_A8R8G8B8;
		d3dpp.BackBufferCount	= 1;
		d3dpp.BackBufferHeight	= desc.Height;
		d3dpp.BackBufferWidth	= desc.Width;
		d3dpp.hDeviceWindow		= desc.WindowInstance;
	}

	// create a default directx device
	HRESULT hr = this->_D3D9->CreateDevice(
			D3DADAPTER_DEFAULT,
			D3DDEVTYPE_HAL,
			desc.WindowInstance,
			D3DCREATE_HARDWARE_VERTEXPROCESSING,
			&d3dpp,
			&this->_D3D9Dev);

	assert(SUCCEEDED(hr));
	assert(this->_D3D9Dev != NULL);

	this->_D3D9Dev->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&this->_BackBuffer);

	this->LoadContent();
}
//##########################################################
void CD3D9RenderEngine::CleanUp (){
	SAFE_RELEASE(this->_Background);
	SAFE_RELEASE(this->_D3D9Dev);
	SAFE_RELEASE(this->_D3D9);
}
//##########################################################
void CD3D9RenderEngine::Render(){
	this->_D3D9Dev->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_ARGB(255, 0, 0, 255),1.0f,0);

	this->_D3D9Dev->Present(NULL,NULL,NULL,NULL);
}
//##########################################################
void CD3D9RenderEngine::LoadContent(void){
	HRESULT result = this->_D3D9Dev->CreateOffscreenPlainSurface(
		800,600,
		D3DFMT_A8R8G8B8,
		D3DPOOL_DEFAULT,
		&this->_Background,
		NULL);
	/*HRESULT result = D3DXLoadSurfaceFromFile(
		this->_Background,
		NULL, 
		NULL,
		"bg.png",
		NULL,
		D3DX_DEFAULT,
		0,
		NULL);*/
}
//##########################################################
//##########################################################
//##########################################################